<template>
  <div class="about">
    <el-form :model="loginForm" :rules="rules" ref="ruleForm" label-width="100px" class="login-container"
             label-position="left">
      <h3 class="login_title">订餐系统后台登陆</h3>
      <el-form-item label="账户名" prop="username">
        <el-input v-model="loginForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input show-password v-model="loginForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loadingButton">登陆</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      loadingButton: false,
      loginForm: {
        username: 'user',
        password: '123456'
      },
      rules: {
        username: [
          {required: true, message: '请输入账户名', trigger: 'blur'},
          {min: 3, max: 10, message: '账户名长度必须在3-10个字符之间', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 12, message: '密码长度必须在6-12个字符之间', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {//如果验证通过
          this.loadingButton = true;
          this.$axios.post("/backstage/login", null, {
            params: {
              username: this.loginForm.username,
              password: this.loginForm.password
            }
          }).then(response => {
            const data = response.data;//获取返回数据
            if (data.success) {//如果登陆成功
              //将token存入
              localStorage.setItem("token",data.data);
              this.$message({
                message: '登陆成功',
                type: 'success'
              });
              //跳转到后台管理系统首页
              this.$router.push('/backstage');
            } else {//如果登陆失败
              //弹出错误信息
              this.$message.error(data.msg);
              //让按钮加载状态回复
              this.loadingButton = false;
            }
          }).catch(error => {
            this.loadingButton = false;
          });
        } else {//如果客户端验证失败
          return false;
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
/*设置表单样式*/
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
}
</style>